﻿//see http://blog.wekeroad.com/blog/my-mvc-starter-template/
//added formatter
namespace AtomSite.WebCore
{
  using System;
  using NLog;

  public class NLogService : ILogService
  {
    private Logger _logger;

    public NLogService()
    {
      _logger = LogManager.GetCurrentClassLogger();
    }

    public void Info(string message)
    {
      _logger.Info(message);
    }

    public void Warn(string message)
    {
      _logger.Warn(message);
    }

    public void Debug(string message)
    {
      _logger.Debug(message);
    }

    public void Error(string message)
    {
      _logger.Error(message);
    }
    public void Error(Exception x)
    {
      if (_logger.IsErrorEnabled)
        Error(LogUtility.BuildExceptionMessage(x));
      //_logger.ErrorException(x.Message, x);
    }
    public void Fatal(string message)
    {
      _logger.Fatal(message);
    }
    public void Fatal(Exception x)
    {
      if (_logger.IsFatalEnabled)
        Fatal(LogUtility.BuildExceptionMessage(x));
        //_logger.FatalException(x.Message, x);
    }

    public void Info(string message, params object[] formatParameters)
    {
      if (_logger.IsInfoEnabled)
        Info(string.Format(message, formatParameters));
    }

    public void Warn(string message, params object[] formatParameters)
    {
      if (_logger.IsWarnEnabled)
        Warn(string.Format(message, formatParameters));
    }

    public void Debug(string message, params object[] formatParameters)
    {
      if (_logger.IsDebugEnabled)
        Debug(string.Format(message, formatParameters));
    }

    public void Error(string message, params object[] formatParameters)
    {
      if (_logger.IsErrorEnabled)
        Error(string.Format(message, formatParameters));
    }

    public void Fatal(string message, params object[] formatParameters)
    {
      if (_logger.IsFatalEnabled)
      Fatal(string.Format(message, formatParameters));
    }
  }
  public class LogUtility
  {
    public static string BuildExceptionMessage(Exception x)
    {
      Exception logException = x;
      if (x.InnerException != null)
        logException = x.InnerException;
      string strErrorMsg = string.Empty;

      if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Request != null)
      {
        if (System.Web.HttpContext.Current.Request.Path != null)
          strErrorMsg += Environment.NewLine + "Error in Path :" + System.Web.HttpContext.Current.Request.Path;
        if (System.Web.HttpContext.Current.Request.RawUrl != null)
          // Get the QueryString along with the Virtual Path
          strErrorMsg += Environment.NewLine + "Raw Url :" + System.Web.HttpContext.Current.Request.RawUrl;
      }

      // Get the error message
      strErrorMsg += Environment.NewLine + "Message :" + logException.Message;

      // Source of the message
      strErrorMsg += Environment.NewLine + "Source :" + logException.Source;

      // Stack Trace of the error

      strErrorMsg += Environment.NewLine + "Stack Trace :" + logException.StackTrace;

      // Method where the error occurred
      strErrorMsg += Environment.NewLine + "TargetSite :" + logException.TargetSite;
      return strErrorMsg;
    }
  }
}